package com.safety.web.controller;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.safety.core.util.Data;
import com.safety.web.entity.Healthcategories;
import com.safety.web.entity.Healthmall;
import com.safety.web.service.HealthcategoriesService;
import com.safety.web.service.HealthmallService;

/**
 * Created by pinky on 2016/12/19.
 */
@Controller
@RequestMapping("/healthmall")
public class HealthmallController {
	@Resource
	private HealthmallService healthmallService;
	@Resource
	private HealthcategoriesService healthcategoriesService;
	// 所有商品类别
	@RequestMapping("/selectHealthmall")
	@ResponseBody
	public Map<String, Object> selectHealthmall() {
		Map<String, Object> result = new LinkedHashMap<>();
		List<Healthmall> list = healthmallService.selectHealthmall();
		Map<String, Object> map;
		List<Map<String, Object>> message = new ArrayList<>();
		try {
			for (Healthmall classList : list) {
				map = new LinkedHashMap<>();
				map.put("heaId", classList.getHealthId());
				map.put("heaname", classList.getHealthmall());
				map.put("heaimg", Data.URL.base +classList.getHealthimg());
				message.add(map);
			}
			result.put("code", 200);
			result.put("message", message);
		} catch (Exception e) {
			result.put("code", 400);
			result.put("message", "发送失败,错误信息:" + e);
		}
		return result;
	}

	@RequestMapping("/selecthealthcategories")
	public @ResponseBody Map<String, Object> selecthealthcategories() {
		Map<String, Object> mapa = new LinkedHashMap<String, Object>();
		List<Healthcategories> list = healthcategoriesService.selecthealthcategories();
		List<Healthcategories> large = healthcategoriesService.selectlargeclass();
		List<Map<String, Object>> list1 = new ArrayList<Map<String, Object>>();
		List<Map<String, Object>> list2 = new ArrayList<Map<String, Object>>();
		List<Object> listObject = new ArrayList<Object>();
		Map<String, Object> map = new LinkedHashMap<String, Object>();
		List<Map<String, Object>> listForArea1 = new ArrayList<>();
		Map<String, Object> listForArea = new LinkedHashMap<String, Object>();

		for (Healthcategories la : large) {
			if (list.size() > 0) {
				for (Healthcategories home : list) {
					if (la.getCateId() == home.getCateId()) {
						if(la.getCid()!=10){
							map.put("imgurl",Data.URL.base +la.getTitle());
							Map<String, Object> map1 = new LinkedHashMap<String, Object>();
							map1.put("id", home.getId());
							map1.put("img",  Data.URL.base +home.getCateimg());
							list1.add(map1);

						}

					}

					if (la.getCateId() == home.getCateId()) {
						if(la.getCid()==10){
							listForArea.put("title",la.getTitle());
							Map<String, Object> listForAreatitle = new LinkedHashMap<String, Object>();
							listForAreatitle.put("id", home.getId());
							listForAreatitle.put("img",  Data.URL.base +home.getCateimg());
							list2.add(listForAreatitle);

						}
					}


				}
				if (list2.size() > 0) {
					listForArea.put("listForAreatitle", list2);
					list2 = new ArrayList<Map<String, Object>>();
					listForArea1.add(listForArea);
				}
				if (list1.size() > 0) {
					map.put("results", list1);
					list1 = new ArrayList<Map<String, Object>>();
					listObject.add(map);
					map = new LinkedHashMap<String, Object>();
					mapa.put("code", 200);
					mapa.put("hint", "获取成功");
				} 
			}else {
				mapa.put("code", 400);
				mapa.put("hint", "获取失败");
			}

		}
		mapa.put("listForArea", listForArea1);
		mapa.put("result", listObject);
		return mapa;
	} 
	@RequestMapping("/carouselfigure")
	public @ResponseBody Map<String, Object> carouselfigure() {
		Map<String, Object> mapa = new LinkedHashMap<String, Object>();
		Map<String, Object> mapForArea4;
		List<Map<String, Object>> listForArea4 = new ArrayList<>();
		List<Healthcategories> list = healthcategoriesService.selecthealthcategories();
		for(Healthcategories home:list){
			if (home.getLocation().substring(0, 5).equals("area4")) {
				mapForArea4 = new LinkedHashMap<>();
				mapForArea4.put("picUrl4", Data.URL.base + home.getCateimg());
				listForArea4.add(mapForArea4);
			}
		}
		mapa.put("code", 200);
		mapa.put("area4", listForArea4);
		return mapa;
	}
}
